import requests
import json

class BigScreenInterface():
    def __init__(self,url):
        self.url = url

    def wenxin_step1(self,content):
        headers = {
            'Authorization': 'Bearer 24.104d5fe3517f4886bd9875156c9394be.2592000.1695972256.282335-32631016',
            'Content-Type': 'application/json'
        }
        payload = json.dumps({
            "chatId": "",
            "content": content,
            "extPrompt": "",
            "promptType": ""
        })
        try:
            response = requests.request("POST", self.url, headers=headers, data=payload, verify=False, timeout=60)
            r_elapsed = response.elapsed.total_seconds()
            return response.text, r_elapsed
        except Exception as e:
            response = json.dumps({'error_msg': str(e)})
            r_elapsed = '请求超时'
            return response, r_elapsed

    def wenxin_step1_ttsAndChart(self,content):
        headers = {
            'Authorization': 'Bearer 24.104d5fe3517f4886bd9875156c9394be.2592000.1695972256.282335-32631016',
            'Content-Type': 'application/json'
        }
        try:
            # print(type(json.loads(content)), json.loads(content))
            payload = json.dumps(json.loads(content),ensure_ascii=False)
            # print(type(payload),payload)
        except Exception as e:
            payload = json.dumps({
                "question": "参数错误",
                "data": []
            },ensure_ascii=False)
        # print(type(payload),payload)
        try:
            response = requests.request("POST", self.url, headers=headers, data=payload.encode('utf-8'), verify=False, timeout=60)
            r_elapsed = response.elapsed.total_seconds()
            # print(response.text)
            return response.text, r_elapsed
        except Exception as e:
            response = json.dumps({'error_msg': str(e)})
            r_elapsed = '请求超时'
            # print(response)
            return response, r_elapsed

    def wenxin_chat_strong(self, content):
        payload = json.dumps({
            "query": content
        })
        headers = {
            'Content-Type': 'application/json'
        }
        try:
            response = requests.request("POST", self.url, headers=headers, data=payload, verify=False,timeout=60)
            r_elapsed = response.elapsed.total_seconds()
            # print(response.text)
            return response.text, r_elapsed
        except Exception as e:
            response = json.dumps({'error_msg': str(e)})
            r_elapsed = '请求超时'
            # print(response)
            return response, r_elapsed


if __name__ == '__main__':
    # url = "https://219.135.214.196:9001/v2x/platform/wenxin/chat"
    url = "http://172.20.137.70:8866/api/chart/query"
    obj = BigScreenInterface(url)
    # obj.wenxin_step1("今天天气怎么样")
    obj.wenxin_step1_ttsAndChart('{"question":"查查最近五天机动车违法停车事件的数量","data":[{"最近五天机动车违法停车事件的数量":20}]}')